package com.dexgdx.game.action.movement;

import com.badlogic.gdx.Gdx;
import com.dexgdx.game.Game;
import com.dexgdx.game.action.AbstractAction;
import com.dexgdx.game.constants.EMoveStopCondition;
import com.dexgdx.game.structure.base.BaseRegion;
import com.dexgdx.game.structure.base.MobileObject;
import com.dexgdx.game.util.pathcalc.IPathCalcSession;

public class MovementAction extends AbstractAction {
	
	public MobileObject movingObject; 
	private BaseRegion initialTarget;
	
	private IPathCalcSession pathCalSession;
	
	public MovementAction(MobileObject movingObject,
			BaseRegion targetRegion, IPathCalcSession pathCalSession) {
		super();
		this.pathCalSession = pathCalSession;
		this.movingObject = movingObject;
		this.initialTarget = targetRegion;
		started = false;
	}
	
	@Override
	public void initialize() {
		Game.gameData.addMovingObject(movingObject);
		movingObject.startMoving(initialTarget);
		pathCalSession.initialize(movingObject, getStopCondition());
		started = true;
	}
	
	@Override
	public void runNextStep() {
		movingObject.setFromRegion(pathCalSession.getNextPosition());
		movingObject.setMoveDirection(pathCalSession.getDirection());
		
		if(isFinished()){
			movingObject.stopMoving();
			Game.gameData.removeMovingObject(movingObject);
		}
		Gdx.graphics.requestRendering();
	}
	
	
	@Override
	public boolean isFinished() {
		return pathCalSession.isCalculationFinished() 
				&& movingObject.hasFinalizedMoving();
	}

	protected EMoveStopCondition getStopCondition(){
		return movingObject.getMoveTarget().isSolidObject() ? EMoveStopCondition.WHEN_TOUCHED : EMoveStopCondition.WHEN_OVER;
	}
	
	protected boolean isFinishedCalculation() {
		return pathCalSession.isCalculationFinished();
	}
}
